$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fypunchcard/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '用户', name: 'userId', index: 'user_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var username ='' ;
                    if(rowdata.userId!=null && rowdata.userId!=''){
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/fyusers/info/"+rowdata.userId,
                            success: function(r){
                                username = r.fyUsers.userName;

                            }
                        });
                    }

                    return username;
                } },
			{ label: '上班打卡时间戳', name: 'sDakatime', index: 's_dakatime', width: 100 },
			{ label: '上班打卡时间', name: 'sDakatimestr', index: 's_dakatimestr', width: 100 },
			{ label: '上班是否在范围内', name: 'sIsScope', index: 's_is_scope', width: 100,formatter: function(item, index) {
                    if (item === 0) {
                        return '<span class="label label-warning">否</span>';
                    }
                    if (item === 1) {
                        return '<span class="label label-success">是</span>';
                    }
                } },
			{ label: '上班是否迟到', name: 'sIsChidao', index: 's_is_chidao', width: 100,formatter: function(item, index) {
                    if (item === 0) {
                        return '<span class="label label-warning">迟到</span>';
                    }
                    if (item === 1) {
                        return '<span class="label label-success">没迟到</span>';
                    }
                } },
			{ label: '上班迟到时长', name: 'sChidaoTime', index: 's_chidao_time', width: 100 },
			{ label: '下班打卡时间戳', name: 'xDakatime', index: 'x_dakatime', width: 100 },
			{ label: '下班打卡时间', name: 'xDakatimestr', index: 'x_dakatimestr', width: 100 },
			{ label: '下班打卡是否在范围', name: 'xIsScope', index: 'x_is_scope', width: 100,formatter: function(item, index) {
                    if (item === 0) {
                        return '<span class="label label-warning">否</span>';
                    }
                    if (item === 1) {
                        return '<span class="label label-success">是</span>';
                    }
                } },
			{ label: '下班是否迟到', name: 'xIsChidao', index: 'x_is_chidao', width: 100,formatter: function(item, index) {
                    if (item === 0) {
                        return '<span class="label label-warning">迟到</span>';
                    }
                    if (item === 1) {
                        return '<span class="label label-success">没迟到</span>';
                    }
                } },
			{ label: '当天日期时间戳', name: 'dateTime', index: 'date_time', width: 100 },
			{ label: '当天日期', name: 'dateTimestr', index: 'date_timestr', width: 100 },
			{ label: '下班提前时长', name: 'xChidaoTime', index: 'x_chidao_time', width: 100 },
			{ label: '规定的上班时间', name: 'sruleTime', index: 'srule_time', width: 100 },
			{ label: '规定的下班时间', name: 'xruleTime', index: 'xrule_time', width: 100 },
			{ label: '总工时', name: 'gongshi', index: 'gongshi', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		fyPunchcard: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fyPunchcard = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.fyPunchcard.id == null ? "sys/fypunchcard/save" : "sys/fypunchcard/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.fyPunchcard),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fypunchcard/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/fypunchcard/info/"+id, function(r){
                vm.fyPunchcard = r.fyPunchcard;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});